﻿// SKNotes - a note taking and gathering tool
// Copyright (C) 2009  Stefan Kueng
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

namespace SKNotes
{
    using System;
    using System.IO;
    using SKNotes.CustomControls;
    using SKNotes.Utilities;
    using System.Collections.Generic;

    public class NoteBook : BindableObject
    {
        private readonly InvokeOC<NoteTag> tags = new InvokeOC<NoteTag>(App.Current.Dispatcher);
        private string noteBookName = null;

        public NoteBook(string notebookName)
        {
            this.NoteBookName = notebookName;
        }

        public string Path
        {
            get
            {
                return NoteList.GetNotebookDirPath() + "\\" + this.noteBookName;
            }
        }

        public string NoteBookName
        {
            get
            {
                return this.noteBookName;
            }

            set
            {
                if ((this.noteBookName != null) && (this.noteBookName != value))
                {
                    DirectoryInfo noteBookDir = new DirectoryInfo(NoteList.GetNotebookDirPath() + "\\" + this.noteBookName);
                    noteBookDir.MoveTo(NoteList.GetNotebookDirPath() + "\\" + value);
                }

                if (this.NoteBookName != value)
                {
                    this.noteBookName = value;
                    this.RaisePropertyChanged("NoteBookName");
                }
            }
        }

        public InvokeOC<NoteTag> Tags
        {
            get { return this.tags; }
        }

        public List<string> TagsList
        {
            get
            {
                List<string> tagslist = new List<string>();
                NoteList.WalkNoteTags(this.tags, (obj) => { tagslist.Add(((NoteTag)obj).TagPath); return false; });
                return tagslist;
            }
        }

        public bool AddTags(InvokeOC<NoteTag> tags)
        {
            return NoteList.WalkTags(tags, (obj) =>
            {
                this.AddTag((NoteTag)obj);
                return false;
            });
        }

        public bool AddTag(NoteTag tag)
        {
            tag.NoteBook = this;
            return NoteList.AddTag(this.Tags, tag);
        }

        public bool RemoveTags(InvokeOC<NoteTag> tags)
        {
            bool ret = false;
            foreach (NoteTag tag in tags)
            {
                ret = this.RemoveTag(tag) || ret;
            }

            return ret;
        }

        public bool RemoveTag(NoteTag tag)
        {
            return NoteList.RemoveTag(this.Tags, tag);
        }
    }
}
